package com.atguigu.exam.service.impl;

import com.atguigu.exam.entity.Exam;
import com.atguigu.exam.entity.ExamRecord;
import com.atguigu.exam.entity.Paper;
import com.atguigu.exam.entity.Question;
import com.atguigu.exam.mapper.ExamRecordMapper;
import com.atguigu.exam.mapper.QuestionMapper;
import com.atguigu.exam.service.ExamRecordService;
import com.atguigu.exam.service.PaperService;
import com.atguigu.exam.vo.StartExamVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 考试记录Service实现类
 * 实现考试记录相关的业务逻辑
 */
@Service
public class ExamRecordServiceImpl extends ServiceImpl<ExamRecordMapper, ExamRecord> implements ExamRecordService {

    @Autowired
    private PaperService paperService;
    @Autowired
    private QuestionMapper questionMapper;
    /**
     * 开始考试 - 创建新的考试记录
     * @param startExamVo 开始考试请求DTO
     * @return 考试记录
     */
    @Override
    public ExamRecord startExam(StartExamVo startExamVo){
        //根据试卷id  状态   名字去查询考试记录
        ExamRecord examRecord = baseMapper.selectOne(
                new LambdaQueryWrapper<ExamRecord>()
                        .eq(ExamRecord::getExamId, startExamVo.getPaperId())
                        .eq(ExamRecord::getStudentName, startExamVo.getStudentName())
                        .eq(ExamRecord::getStatus,"进行中")
        );
        if (examRecord != null){
            //正在考试中
            return examRecord;
        }
         examRecord = new ExamRecord();
        //设置考试id
        examRecord.setExamId(startExamVo.getPaperId());
        //设置考试姓名
        examRecord.setStudentName(startExamVo.getStudentName());
        //设置考试开始时间
        examRecord.setStartTime(LocalDateTime.now());
        //设置窗口切换次数
        examRecord.setWindowSwitches(0);
        //设置考试状态为进行中
        examRecord.setStatus("进行中");
        //考试插入 数据
        baseMapper.insert(examRecord);
        return examRecord;
    }

}